package com.threeox.commonlibrary.util;

import android.app.Dialog;
import android.content.Context;
import android.view.View;

import com.threeox.commonlibrary.ui.dialog.CommonModelDialogBuilder;
import com.threeox.commonlibrary.ui.dialog.impl.CommonDialogClickListener;
import com.threeox.commonlibrary.ui.view.loadview.ILoadDialog;
import com.threeox.commonlibrary.ui.view.loadview.LoadDialog;

/**
 * 
 * @ClassName: DialogUtil
 * 
 * @Description: TODO()
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/7/10 10:46
 * 
 * @version 1.0
 * 
 */
public class DialogUtil {

    /**
     * 展示dialog
     *
     * @param dialog
     * @param message
     * @param context
     * @return
     */
    public static ILoadDialog showLoadDialog(ILoadDialog dialog, CharSequence message, Context context) {
        if (dialog == null) {
            dialog = new LoadDialog(context);
        }
        dialog.setMessage(message);
        if (!dialog.isShowing()) {
            dialog.show();
        }
        return dialog;
    }

    /**
     * 隐藏Dialog
     *
     * @param dialog
     */
    public static void dismissDialog(ILoadDialog dialog) {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 显示消息提示框
     *
     * @param context
     * @param title
     * @param msg
     * @param rightText
     * @param rightClickListener
     * @return
     */
    public static CommonModelDialogBuilder showMessageDialog(Context context, String title, String msg, String rightText,
                                                             CommonDialogClickListener rightClickListener) {
        return showMessageDialog(context, title, msg, null, null, rightText,
                rightClickListener, null, null);
    }

    public static CommonModelDialogBuilder showMessageDialog(Context context, String title, String msg, String leftText,
                    CommonDialogClickListener leftClickListener,  String rightText, CommonDialogClickListener rightClickListener) {
        return showMessageDialog(context, title, msg, leftText, leftClickListener, rightText, rightClickListener, null, null);
    }

    /**
     * 显示消息提示框
     *
     * @param context
     * @param title
     * @param msg
     * @return
     */
    public static CommonModelDialogBuilder showMessageDialog(Context context, String title, String msg, String leftText,
                       CommonDialogClickListener leftClickListener, String rightText, CommonDialogClickListener rightClickListener, String centerText,
                                                             CommonDialogClickListener centerClickListener) {
        CommonModelDialogBuilder builder = showTitle(context, title);
        builder.setContent(msg);
        setButton(leftText, leftClickListener, rightText, rightClickListener, centerText, centerClickListener, builder);
        builder.initDialog().show();
        return builder;
    }

    /**
     * 加载View的Dialog
     *
     * @param context
     * @param title
     * @param view
     * @return
     */
    public static CommonModelDialogBuilder showViewDialog(Context context, String title, View view) {
        CommonModelDialogBuilder builder = new CommonModelDialogBuilder(context);
        builder.setTitle(title);
        builder.setContentView(view);
        builder.setShowButtonLayout(false);
        return builder;
    }
    /**
     * 加载View的Dialog
     *
     * @param context
     * @param title
     * @param view
     * @return
     */
    public static CommonModelDialogBuilder showViewDialog(Context context, int title, View view) {
        CommonModelDialogBuilder builder = new CommonModelDialogBuilder(context);
        builder.setTitle(title);
        builder.setContentView(view);
        builder.setShowButtonLayout(true);
        return builder;
    }

    /**
     * 显示Title
     *
     * @return
     */
    private static CommonModelDialogBuilder showTitle(Context context, String title) {
        CommonModelDialogBuilder builder = new CommonModelDialogBuilder(context);
        builder.setTitle(title);
        return builder;
    }

    /**
     * 设置按钮
     *
     * @param leftText
     * @param leftClickListener
     * @param rightText
     * @param rightClickListener
     * @param centerText
     * @param centerClickListener
     */
    private static void setButton(String leftText, CommonDialogClickListener leftClickListener, String rightText, CommonDialogClickListener rightClickListener,
                                  String centerText, CommonDialogClickListener centerClickListener, CommonModelDialogBuilder builder) {
        if (leftClickListener != null) {
            builder.setLeftButton(leftText, leftClickListener);
        } else {
            builder.setLeftButton("取消", new CommonDialogClickListener() {
                @Override
                public void onClick(CommonModelDialogBuilder builder, Dialog dialog, String buttonText) {
                    dialog.dismiss();
                }
            });
        }
        if (rightClickListener != null) {
            builder.setRightButton(rightText, rightClickListener);
        }
        if (centerText != null) {
            builder.setCenterButton(centerText, centerClickListener);
        }
    }

}
